<?php
namespace Addons\UserAnalysis\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
    }
    /*******查出****/
    //add [hours,week,day,month,year]
    public function get_count($time = "", $add = "day")
    {
        $where = "1";
        $model = BoxModel('system_member');
        if ($time) {
            $start_date = $time;
            $end_date = strtotime(date("Y-m-d H:i:s", $start_date) . " +1{$add}");
            $where .= " and ('$start_date' < reg_time  and reg_time< '$end_date')";
        }
        $data = $model->where($where)->count();
        $r['count'] = $data ? $data : 0;
        $type_array = array('1' => '男', '2' => '女', '3' => '保密');
        foreach ($type_array as $k => $one) {
            $sql = $where . " and (sex='$k')";
            $data = $model->where($sql)->count();
            $r[$k] = $data ? $data : 0;
        }
        return $r;
    }

    /******用户统计****/
    public function index()
    {
        /****今日新增***/
        $now_time = strtotime(date("Y-m-d"));
        $this->now_count = $this->get_count($now_time);
        /******昨天新增***/
        $now_time = strtotime(date("Y-m-d", strtotime("-1 day")));
        $this->yes_count = $this->get_count($now_time);
        /*******总新增**/
        $this->all_count = $this->get_count();
        /*****************最近几天新增*****************/
        $this->d = 7;
        if (S("UserAnalysis_d")) {
            $this->d = S("UserAnalysis_d");
        }
        if (I('d')) {
            $this->d = substr(I('d'), 0, 2);//默认是七天的
            S("UserAnalysis_d", $this->d);
        }
        $this->options_day = $this->get_data($this->d, "day", "Y-m-d");
        /***************最近几个月的数据***************/
        $this->m = 12;
        if (S("UserAnalysis_m")) {
            $this->m = S("UserAnalysis_m");
        }
        if (I('moth')) {
            $this->m = substr(I('moth'), 0, 2);
            S("UserAnalysis_m", $this->m);
        }

        $this->options_month = $this->get_data($this->m, "month", "Y-m");
        /***************最近三年的数据***************/
        $this->y = 3;
        if (S("UserAnalysis_y")) {
            $this->y = S("UserAnalysis_y");
        }
        if (I('y')) {
            $this->y = substr(I('y'), 0, 1);
            S("UserAnalysis_y", $this->y);
        }
        $this->options_year = $this->get_data($this->y, "year", "Y-m");
        $this->admin_display();
    }

    function get_data($i, $type = "day", $form = "Y-m-d")
    {
        $this->type_array = array('1' => '男', '2' => '女', '3' => '保密');
        $end_date = date($form, time());
        $start_date = date($form, strtotime("-$i $type"));
        $day_line_options = array(); //折线图
        $options_day = array();//扇形图
        for ($i = $i - 1; $i > -1; $i--) {
            $date = date($form, strtotime("-$i $type"));
            $categories[] = $date;
        }
        foreach ($categories as $one) {
            $r = $this->get_count(strtotime($one), $type);
            $data[] = $r['count'];
            $options_day_data['all'] += $r['count'];
            foreach ($this->type_array as $k => $one2) $options_day_data[$k] += $r[$k]; //新增分布
            $categories_str[] = "'" . $one . "'";
        }
        $day_line_options['xAxis']['categories'] = implode(",", $categories_str);
        $day_line_options['series']['data'] = implode(",", $data);
        $day_line_options['title']['text'] = "{$start_date}~{$end_date} 用户增长图";
        /******最近几天新增扇形图*********/
        //dump($options_day_data);
        $options_day['title']['text'] = "{$start_date}~{$end_date} 用户分布图 (总新增+" . $options_day_data['all'] . ")";
        $data = array();
        foreach ($this->type_array as $k => $one) {
            $data[] = "['" . $one . "(" . $options_day_data[$k] . ")', " . $options_day_data[$k] . "]";
        }
        $options_day['series']['data'] = implode(",", $data);
        $options_day = $options_day; //扇形图数据
        $data = array(
            'options_day' => $options_day,
            'day_line_options' => $day_line_options,
        );
        return $data;
    }


}